﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Web.Mvc;
using WorkData.ElasticSearch.Config;
using WorkDataEs.WorkDataElasticSearchs.Contents;
using WorkDataEs.WorkDataElasticSearchs.Contents.Dto;

namespace ElasticSearchWeb.Controllers
{
    public class HomeController : Controller
    {
        private readonly IContentService service;
        public HomeController(IContentService iContentService)
        {
            service = iContentService;
        }
        public ActionResult Index(string secch, int PageSize = 50)
        {
            var result = new List<Content>();
            try
            {
                Stopwatch sb = new Stopwatch();
                //sb.Start();
                //service.BlukIndex(Contents, "index_cus");
                //sb.Stop();
                //Console.WriteLine("写入成功:" + sb.ElapsedMilliseconds.ToString());
                //复杂查询
                var request = new RequestContentDto
                {
                    PageSize = PageSize,
                    SearchKey = secch,

                };

                #region 高亮

                var highlightConfig = new HighlightConfig<Content>
                {
                    Tag = "i",
                    HighlightConfigExpression = new List<Expression<Func<Content, object>>>
                {
                    x=>x.Title
                }
                };
                request.HighlightConfigEntity = highlightConfig;
                if (service != null)
                {
                    Stopwatch sb2 = new Stopwatch();
                    sb2.Start();
                    var data = service.Search(1, request.PageSize, request);
                    sb2.Stop();
                    result.AddRange(data.Contents);
                    Console.WriteLine("查询数量" + data.Contents.Count + "----查询时间：" + sb2.ElapsedMilliseconds);
                    ViewBag.pag = "查询数量" + data.Contents.Count + "----查询时间：" + sb2.ElapsedMilliseconds + "----总数：" + data.Records;
                    //Console.WriteLine(Newtonsoft.Jschon.JsonConvert.SerializeObject(data.Contents) + "时间：" + sb2.ElapsedMilliseconds);
                }
                else
                {
                    Console.WriteLine("null");
                }

                ////删除
                //service.DeleteByQuery("123852");

                ////更新
                //service.UpdateByKey("123754", new Content
                //{
                //    Key = "123"
                //});

                #endregion 高亮
            }
            catch (Exception e)
            {

                Console.WriteLine($"异常:" + e.Message + e.InnerException.Message);
            }
            return View(result);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}